﻿using System;
using System.Windows.Forms;
using OpenNETCF.AppSettings;

namespace Pozr
{
    /// <summary>
    /// Delegate that refreshes the map panel
    /// </summary>
    public delegate void MapRefreshDelegate();
    public delegate void LogMessageDelegate(string message);
    public delegate void RefreshSignalStateDelegate();

    /// <summary>
    /// Holds a static instance of global program stuff
    /// </summary>
    public class Middleware
    {
        public static MainForm mainFrm;
        public static DatabaseClient Database;
        public static LogMessageDelegate WriteLog;
        public static RefreshSignalStateDelegate refreshSignalState;

        public static Pozr.PluginServices Plugins;

        public static Map.MapPanel MapDisplay;
        public static Pozr.Markers Markers;

        public static Pozr.PositionHandler PositionHandler;
        public static Pozr.EventEngine EventEngine;
        public static Pozr.ServiceWrapper Server;

        public static readonly string ConfigurationDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Pozr";
        public static readonly string ApplicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        public static SettingsWrapper Settings;

        public Middleware(MainForm _mainFrm)
        {
            try
            {
                
                Database = new SqlCeClient();
            }
            catch (MissingMethodException ex)
            {
                WriteLog("SQL CE is not installed! Local database disabled.");
                Database = new DummyClient();
            }

            if(!System.IO.Directory.Exists(ConfigurationDirectory))
            {
                System.IO.Directory.CreateDirectory(ConfigurationDirectory);
            }
            Settings = new SettingsWrapper(new SettingsFile(ConfigurationDirectory + "\\settings.xml")); // BN: SettingsFile konstruktor lassú

            Server = new ServiceWrapper();
            Plugins = new PluginServices();
            PositionHandler = new PositionHandler();
            EventEngine = new EventEngine();


            MapDisplay = new Map.MapPanel(Map.MapPanel.ButtonStateEnum.Show);
            Markers = new Markers();

            mainFrm = _mainFrm;
        }

        /// <summary>
        /// Class that stores the state of an asynchronous request
        /// </summary>
        public class RequestState
        {
       
            /// <summary>
            /// Holds the request
            /// </summary>
            public System.Net.WebRequest Request;
            /// <summary>
            /// Store any data in this
            /// </summary>
            public object Data;
            /// <summary>
            /// Holds the UrlString to match up results (Database lookup, etc).
            /// </summary>
            public string SiteUrl;
            /// <summary>
            /// Constructor of the RequestState class
            /// </summary>
            /// <param name="request">Request</param>
            /// <param name="data">Object Data</param>
            /// <param name="siteUrl">URL string</param>
            public RequestState(System.Net.WebRequest request, object data, string siteUrl)
            {
                this.Request = request;
                this.Data = data;
                this.SiteUrl = siteUrl;
            }
        }
    }
}
